package dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import dto.TestScoreDTO;

public class TestScoreDAO extends DatabaseHelper {
	
    public static final String TABLE_TESTSCORE = "Test_Score";
    
    public static final String TESTSCORE_ID = "ID";
    public static final String TESTSCORE_TESTID = "TestID";
    public static final String TESTSCORE_TITLEID = "TitleID";
    public static final String TESTSCORE_TOTALSCORE = "TotalScore";
    
	public TestScoreDAO(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

    public void addTestScore(TestScoreDTO testScoreDTO) 
    {
    	// Open database
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(TESTSCORE_TESTID, testScoreDTO.getTestID());
	        values.put(TESTSCORE_TITLEID, testScoreDTO.getTitleID());
	        values.put(TESTSCORE_TOTALSCORE, testScoreDTO.getTotalScore());
	        
	        // Inserting rows
	        open();
	        DatabaseHelper.myDataBase.insert(TABLE_TESTSCORE, null, values);
	
	        // Close database connection
	        DatabaseHelper.myDataBase.close();
        }
        catch (Exception ex) {}
    }         
    
    public TestScoreDTO getTestScore(int id) {
        try {
	        Cursor cursor = DatabaseHelper.myDataBase.query(TABLE_TESTSCORE, new String[] { TESTSCORE_ID,
	        		TESTSCORE_TESTID, TESTSCORE_TITLEID, TESTSCORE_TOTALSCORE }, TESTSCORE_ID + "?",
	            new String[] {String.valueOf(id) }, null, null, null, null);
	        if (cursor != null)
	            cursor.moveToFirst();
	        
	        TestScoreDTO testScoreDTO = new TestScoreDTO(
	                Integer.parseInt(cursor.getString(0)),
	                Integer.parseInt(cursor.getString(1)),
	                Integer.parseInt(cursor.getString(2)),
	                Integer.parseInt(cursor.getString(3)));
	        return testScoreDTO;
        }
        catch (Exception e) {
        	return null;
        }
    }
        
    public List<TestScoreDTO> getAllTestScores() {
    	try {
	        List<TestScoreDTO> testScoreList = new ArrayList<TestScoreDTO>();
	        
	        // Select all query
	        String selectQuery = "SELECT * FROM " + TABLE_TESTSCORE;
	        open();
	        Cursor cursor = DatabaseHelper.myDataBase.rawQuery(selectQuery, null);
	        
	        // Looping through all rows and adding to list
	        
	        if (cursor.moveToFirst()) {
	            do{
	            	TestScoreDTO testScoreDTO = new TestScoreDTO(
	    	                Integer.parseInt(cursor.getString(0)),
	    	                Integer.parseInt(cursor.getString(1)),
	    	                Integer.parseInt(cursor.getString(2)),
	    	                Integer.parseInt(cursor.getString(3)));
	            
	            	testScoreList.add(testScoreDTO);
	            } while (cursor.moveToNext());
	        }
	        close();
	        return testScoreList;
    	}
    	catch (Exception e) {
			return null;
		}
    }
    
    public boolean updateTestScore(TestScoreDTO testScoreDTO) {
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(TESTSCORE_ID, testScoreDTO.getID());
	        values.put(TESTSCORE_TESTID, testScoreDTO.getTestID());
	        values.put(TESTSCORE_TITLEID, testScoreDTO.getTitleID());
	        values.put(TESTSCORE_TOTALSCORE, testScoreDTO.getTotalScore());
	        open();
	        // Updating row
	        int isSucceed = DatabaseHelper.myDataBase.update(TABLE_TESTSCORE, values, TESTSCORE_ID + " = ?",
	                new String[] { String.valueOf(testScoreDTO.getID()) });
	        close();
	        if (isSucceed > 0) {
	        	return true;
	        }
	        	
	        return false;
        }
        catch (Exception ex) { return false; }
    }
    
    public void deleteTestScore(TestScoreDTO testScoreDTO) {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_TESTSCORE, TESTSCORE_ID + " = ?",
                new String[] { String.valueOf(testScoreDTO.getID()) });
    		DatabaseHelper.myDataBase.close();
    	}
    	catch (Exception ex) {}
    }
    
    public void deleteAllTestScores() 
    {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_TESTSCORE, null, null);
    	}
    	catch (Exception ex) {}
    }
    
    public int getTestScoreCount()
    {
    	try {
	        String countQuery = "SELECT * FROM " + TABLE_TESTSCORE;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	        
	        // Return count
	        return cursor.getCount();
    	}
    	catch (Exception ex) {return -1;}
    }
}

